<?php
class Ctl_Lottery
{
	public function getUserInfo($act_id)
	{
		try {
			$act_user = Model_ActUser::getInstance();
			/*@var $act_user Model_ActUser*/
			$friends = $act_user->getFriendsList(true);
			
			$win_times = Model_ActMp::batchGetWinTimes($act_id, array_keys($friends));
			$result_friends = array();
			foreach ($win_times as $uin => $value){
				if ($value > 0) {
					$result_friends[$uin] = array(
						"NICKNAME" 	=> $friends['NICKNAME'],
						"WIN_TIMES"	=>	$value,
 					);
				}
			}
			unset($friends);
			
		} catch (Model_ActEx $e) {
			V::dump($e->getData());
			die($e->getMessage());
		} catch (Exception $e){
			//to do 
			die($e->getMessage());	
		}	
	}
	
	public function getActInfo($act_id)
	{
		
	}
	
	public function play($act_id)
	{		
		try {
			//init user object and info object
			$act_user = Model_ActUser::getInstance();
			$act_info = Model_ActInfo::getInstance($act_id);
			
			//check rules, if user can play the game
			$ruler = Model_ActRuler::getInstanceFromActInfo($act_info);
			$ruler->check($act_user);
			
			//generate MP_ID; user with different level will access different game
			$mp_id = $act_info->getAttr('MP_ID');		
			$user_level = $act_user->getAttr('LEVEL');
			$mp_id = $mp_id . $user_level;

			//let user play the game
			$uin = $act_user->getAttr("UIN");
			$result = Model_ActMP::getInstance($mp_id)->bindUser($uin)->play();
			
			echo json_encode($result);
		} catch (Model_ActEx $e) {
			v::dump($e->getData());
			die($e->getMessage());
		} catch (Exception $e){
			//to do 
			die($e->getMessage());	
		}
	}
}